#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QDebug>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->timeEdit->setDisplayFormat("A HH:mm::ss");
    ui->dateEdit->setDisplayFormat("yyyy年MM月dd日");
    fTimer = new QTimer(this);
    fTimer->stop();
    fTimer->setInterval(1000);// 设置定时周期、单位、时间
    ui->progressBar->setValue(0);
    connect(fTimer, SIGNAL(timeout()), this, SLOT(slot_timer_timeout()));
    this->setWindowTitle("时间、定时器、日历");

}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_pushButton_clicked()
{
    QDateTime currentDateTime = QDateTime::currentDateTime();
    ui->timeEdit->setTime(currentDateTime.time());
    ui->timeLine->setText(currentDateTime.toString("A hh:mm:ss"));
    ui->dateEdit->setDate(currentDateTime.date());
    ui->dateLine->setText(currentDateTime.toString("yyyy年MM月dd日"));
    ui->dateTimeEdit->setDateTime(currentDateTime);
    ui->dateAndTimeLine->setText(currentDateTime.toString("yyyy-MM-dd hh:mm:ss"));

}

void MainWindow::on_setTimeBtn_clicked()
{
    QString str = ui->timeLine->text();
    qDebug()<< str;
    str = str.trimmed();
    if(!str.isEmpty()){
        QTime time = QTime::fromString(str, "A hh:mm:ss");
        ui->timeEdit->setTime(time);
    }
}

void MainWindow::on_setDateBtn_clicked()
{
    QString str = ui->dateLine->text();
    str = str.trimmed();
    if(!str.isEmpty()){
        QDate date = QDate::fromString(str, "yyyy年MM月dd日");
        ui->dateEdit->setDate(date);
    }
}

void MainWindow::on_setTAndDBtn_clicked()
{
    QString str = ui->dateAndTimeLine->text();
    str = str.trimmed();
    if(!str.isEmpty()){
        QDateTime dateTime = QDateTime::fromString(str, "yyyy/mm/dd hh:mm:ss");
        ui->dateTimeEdit->setDateTime(dateTime);
    }
}

void MainWindow::on_calendarWidget_selectionChanged()
{
    QDate dt = ui->calendarWidget->selectedDate();
    QString str = dt.toString("yyyy年MM月dd日");
    ui->calendarLine->setText(str);
}

void MainWindow::on_eixtBtn_clicked()
{
    this->close();
}

void MainWindow::slot_timer_timeout(){
    QTime currTime = QTime::currentTime();
    ui->hourLcd->display(currTime.hour());
    ui->minuteLcd->display(currTime.minute());
    ui->secondLcd->display(currTime.second());

    int va = currTime.second() * 100 / 60;
    va += 100/ 60;
    if(va >100){
        va = 0;
    }
    ui->progressBar->setValue(va);
}

void MainWindow::on_startBtn_clicked()
{
    fTimer->start();
    fTimeCounter.start();
    ui->startBtn->setEnabled(false);
    ui->stopBtn->setEnabled(true);
    ui->setPeriodBtn->setEnabled(false);
}

void MainWindow::on_stopBtn_clicked()
{
    fTimer->stop();
    int tmeLost = fTimeCounter.elapsed(); // 毫秒数
    int ms = tmeLost % 1000;
    int sec = tmeLost / 1000;
    QString str = QString::asprintf("时间流逝: %d秒, %d毫秒", sec, ms);
    ui->timeLost->setText(str);
    ui->startBtn->setEnabled(true);
    ui->setPeriodBtn->setEnabled(true);
    ui->stopBtn->setEnabled(false);
}

void MainWindow::on_setPeriodBtn_clicked()
{
    int period = ui->periodSpin->value();
    fTimer->setInterval(period);
}
